package com.paohe.lianxin_pda.production_line.chengpPei;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.paohe.lianxin_pda.R;
import com.paohe.lianxin_pda.utils.spinner.SpinnerUtils;

import java.util.ArrayList;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class ChengPei extends AppCompatActivity implements VerticalTabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    private ImageView back;//返回
    private TextView title,chooseTime;//标题名称
    private SpinnerUtils mySpinner;//自定义下拉框组件
    private VerticalTabLayout mTab;
    private ViewPager mVp;
//    private String[] mTitlesList =new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private FragmentPagerAdapter mAdapter;
    private String[] mTitlesList =new String[]{"A","B","C"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheng_pei);

        back = findViewById(R.id.back);
        title = findViewById(R.id.title_name);
        chooseTime = findViewById(R.id.choose_time);
        mTab = findViewById(R.id.cp_tab);
        mVp = findViewById(R.id.cp_vp);

        //tab相关-start

        //创建适配器、适配器继承自FragmentPagerAdapter
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            // titles作为滑动标题栏，动态添加数据进去就可以了
            String[] titles = mTitlesList;

            @Override
            public Fragment getItem(int position) { // 开始动态添加fragment
                FragmentTab fragment = new FragmentTab();
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                bundle.putString("title", titles[position]);
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() { // 添加fragment个数
                return titles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) { // 添加fragment标题
                return titles[position];
            }
        };

        mVp.setAdapter(mAdapter);//绑定适配器
        mVp.setOffscreenPageLimit(26);
        //Tab和ViewPager关联
        mTab.setupWithViewPager(mVp);
        //tab相关-end


        //获取下拉列表1的数据源
        final String[] mItems = getResources().getStringArray(R.array.sprinnerarr1);

        //下拉框点击事件
        chooseTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySpinner = new SpinnerUtils(ChengPei.this, chooseTime, mItems, new SpinnerUtils.MyItemClickListener() {
                    @Override
                    public void onClick(View view) {
                        //子项的点击事件
                        //TODO
                    }
                });
                mySpinner.showPopupWindow();
            }
        });

        //给标签赋值
        title.setText(R.string.chengpei_cp);

        //返回按钮
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onTabSelected(TabView tab, int position) {
        mVp.setCurrentItem(position);
    }

    @Override
    public void onTabReselected(TabView tab, int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTab.setTabSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
